package com.chanyachting.emall.action.mainproduct;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Businesscategory;
import com.chanyachting.emall.domain.Productcategory;
import com.chanyachting.emall.form.ProductcategoryForm;
import com.chanyachting.emall.service.BusinesscategoryService;
import com.chanyachting.emall.service.ProductcategoryService;
import com.chanyachting.emall.serviceImpl.BusinesscategoryServiceImpl;
import com.chanyachting.emall.serviceImpl.ProductcategoryServiceImpl;
import com.chanyachting.emall.util.RequestUtil;

public class MainProductAction extends DispatchAction {

	
	
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int id=Integer.parseInt(new RequestUtil(request).getParameter("id", "0"));
		
		ProductcategoryService pcategorys=new ProductcategoryServiceImpl();
		
		Productcategory pcategory=new Productcategory();
		
		pcategory.setId(id);
		
		pcategory=pcategorys.getOneProductcategory(pcategory);
		
		new RequestUtil(request).setAttribute("productCategory", pcategory);
		
		return mapping.findForward("show");
	}
	
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int id=Integer.parseInt(new RequestUtil(request).getParameter("id", "0"));
		
		Productcategory pcategory=new Productcategory();
		
		if(request.getMethod().equals("POST")){
			
			ProductcategoryForm  pcForm=(ProductcategoryForm)form;
			
			//如果验证成功
			if(pcForm.validate(mapping, request).isEmpty()){
				
				Productcategory pc=new Productcategory();
				
				BeanUtils.copyProperties(pc, pcForm);
				
				ProductcategoryService pcategorys=new ProductcategoryServiceImpl();
				
				pcategorys.updateProductcategory(pc);
				
				new RequestUtil(request).setSuccessMsg();
			}
			
		}
		
		//重新加载主营商品对象
		ProductcategoryService pcategorys=new ProductcategoryServiceImpl();
		
		pcategory.setId(id);
		
		pcategory=pcategorys.getOneProductcategory(pcategory);
		
		new RequestUtil(request).setAttribute("productCategory", pcategory);
		
		
		//重新加载行业次分类列表
		BusinesscategoryService bcs=new BusinesscategoryServiceImpl();
		
		List list=bcs.getAllBusinesscategory();
		
		new RequestUtil(request).setAttribute("businesscategoryList", list);
		
		
		return mapping.findForward("update");
	}
	
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int id=Integer.parseInt(new RequestUtil(request).getParameter("id", "0"));
		
		int bcId=Integer.parseInt(new RequestUtil(request).getParameter("bcId", "0"));
		
		Productcategory pc=new Productcategory();
		
		pc.setId(id);
		pc.setBusinesscategoryId(bcId);
		pc.setName("");
		
		ProductcategoryService pcategorys=new ProductcategoryServiceImpl();
		
		pcategorys.delProductcategory(pc);
		
		//删除成功，重新加载列表
		return this.search(mapping, form, request, response);
	}
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		if(request.getMethod().equals("POST")){
			
			ProductcategoryForm  pcForm=(ProductcategoryForm)form;
			
			//如果验证成功
			if(pcForm.validate(mapping, request).isEmpty()){
				
				Productcategory pc=new Productcategory();
				
				BeanUtils.copyProperties(pc, pcForm);
				
				ProductcategoryService pcategorys=new ProductcategoryServiceImpl();
				
				pcategorys.addProductcategory(pc);
				
				new RequestUtil(request).setSuccessMsg();
			}
			
		}
		
		//加载行业次分类列表
		BusinesscategoryService bcs=new BusinesscategoryServiceImpl();
		
		List list=bcs.getAllBusinesscategory();
		
		new RequestUtil(request).setAttribute("businesscategoryList", list);
		
		return mapping.findForward("create");
	}
	
	
	
	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ProductcategoryService pcategorys=new ProductcategoryServiceImpl();
		
		List list=pcategorys.getAllProductcategory();
		
		new RequestUtil(request).setAttribute("productcategoryList", list);
		
		return mapping.findForward("list");
	}
	
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		int bcId=Integer.parseInt(new RequestUtil(request).getParameter("bcId", "0"));
		
		BusinesscategoryService bcs=new BusinesscategoryServiceImpl();
		
		Businesscategory bc=new Businesscategory();
		
		bc.setId(bcId);
		
		List list=bcs.getProductcategoryByBusinesscategory(bc);
		
		new RequestUtil(request).setAttribute("productcategoryList", list);
		
		return mapping.findForward("list");
	}
	
	

}
